import { upgradeHttp } from '/@/utils/http/axios';
import { DbInfo, ExcecuteRecordResultModel } from '../model/upgradeDbModel';

enum Api {
  UPLOAD_SQL_FILE = 'basic-api/saaslanding-upgrade/v1/upgrade/database/uploadSql',
  GET_DB_INFO = '/v1/upgrade/database/dataBaseInfo',
  DELETE_UPGRADE_RECORD = '/v1/upgrade/database/delete',
  DOWNLOAD_UPGRADE_RECORD = '/v1/upgrade/database/detail',
  GET_TASK_LIST = '/v1/upgrade/database/getList',
  EXCECUTE_SQL = '/v1/upgrade/database/migrate',
}

// 根据应用ID、租户Id 信息获取DB资源信息
export const getDbInfo = (params: { tenantId: string; appId: string }) => {
  return upgradeHttp.get<DbInfo>({ url: Api.GET_DB_INFO, params });
};

// 获取所有非内置租户列表
export const deleteUpgradeRecord = (params: { id: string }) => {
  return upgradeHttp.get({ url: Api.DELETE_UPGRADE_RECORD, params });
};

// 根据ID 下载sql文件
export const downloadUpgradeRecord = (params: { id: string; type: number }) => {
  return upgradeHttp.get<string>({ url: Api.DOWNLOAD_UPGRADE_RECORD, params });
};

// 查看任务列表
export const getTaskList = (params: { current: number; size: number; appId: string; tenantId: string }) => {
  if (!params.appId || !params.tenantId) {
    return;
  }
  return upgradeHttp.get<ExcecuteRecordResultModel>({ url: Api.GET_TASK_LIST, params });
};

// 执行迁移
export const excecuteSql = (params: { appId: string; id: string; tenantId: string; type: number }) => {
  return upgradeHttp.post({ url: Api.EXCECUTE_SQL, params });
};
// 上传sql文件
export const uploadSqlFile = (params: {
  data: {
    appId: string;
    tenantId: string;
    type: number;
    description: string;
  };
  file: unknown;
  rollbackFile?: unknown;
}) => {
  return upgradeHttp.uploadFile(
    {
      url: Api.UPLOAD_SQL_FILE,
    },
    params
  );
};
